import axios from 'axios';
import apiConfig from './api.config';
let path = [28, 246].includes(apiConfig.serverNum) ? 'Home' : 'Qdmes/QDMES';

function send (webUrl, functionname, dataContent) {
    return new Promise((resolve, reject) => {

        // var config = {
        //     method: 'post',
        //     url: apiConfig.baseUrlTPX + path,
        //     headers: {
        //         'User-Agent': '',
        //         'Content-Type': 'application/json',
        //         'Accept': '*/*',
        //         'Host': '',
        //         'Connection': 'keep-alive',
        //     },
        //     data: data,
        // };

        let params = [];
        for (let key in dataContent) {
            params.push({ name: key, value: dataContent[key] });
        }
        let postData = { url: webUrl, functionname: functionname, json: JSON.stringify(params) };
        // console.log('postData :>> ', postData);
        axios({
            url: `./${path}/FileOperation`,
            method: 'post',
            data: postData,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            dataType: "text",
        }).then((response) => {
            // 成功例子 {"errcode":0,"errmsg":"ok"}
            // 失败例子 {"errcode":45047,"errmsg":"out of response count limit rid: 67c80a18-3dd57dbe-098838d0"}
            // console.log('response.data :>> ', response.data);
            if (typeof response.data == 'string') {
                let jsonObj = JSON.parse(response.data);
                if (jsonObj.errcode == 0) {
                    resolve('成功');
                }
                else {
                    resolve(jsonObj.errmsg);
                }
            }
            else {
                resolve(response.data);
            }
        }).catch((err) => {
            console.log('err :>> ', err);
            reject(err);
        });



        //处理图片路径
        // let params = { dataContent };

        // axios({
        //     url: `./${path}`,
        //     method: isGet ? 'get' : 'post',
        //     params: isGet ? params : {},
        //     data: isGet ? {} : params,
        //     dataType: "text",
        //     baseURL: apiConfig.baseUrl28,
        // }).then((response) => {
        //     resolve(response.data);
        // }).catch(err => {
        //     console.log('err :>> ', err);
        //     reject(err);
        // });
    });
}
const myAxiosWXPost = {
    get (webUrl, functionname, dataContent) {
        dataContent = dataContent ? dataContent : '{}';
        return send(webUrl, functionname, dataContent);
    },
    post (webUrl, functionname, dataContent) {
        dataContent = dataContent ? dataContent : '{}';
        return send(webUrl, functionname, dataContent);
    },
};
export default myAxiosWXPost;
